import './public-path'
import { createApp, h } from "vue";
import AppComponent from "./App.vue";
import "./registerServiceWorker";
import iCreateRouter from "./router";
import { createPinia } from 'pinia'
import type { App } from 'vue'

let instance: App | null

declare global {
  interface Window {
    __WUJIE_UNMOUNT: Function
    __WUJIE_MOUNT: Function
    __POWERED_BY_WUJIE__?: boolean;
    __POWERED_BY_QIANKUN__?: boolean;
    __SUB_APP_CACHE__?: App
  }
}
const router = iCreateRouter()
const pinia = createPinia()
function render() {
  instance = createApp(AppComponent)
  instance.use(pinia).use(router)
  instance.mount('#cso-app')
}

if (!window.__POWERED_BY_WUJIE__) {
  render()
} else {
  window.__WUJIE_MOUNT = () => {
    ;(window as any).$wujie?.bus.$on('cso-router-change', (path: string) => {
      if (!path) {
        router.push('/')
        return
      }
      const currentPath = window.location.pathname;
      console.log('cso', currentPath)
      if (currentPath.startsWith('/cso') && currentPath !== path) {
        router.push(currentPath)
        return
      }
      router.push(path)
    })
    render();
    console.log('cso', window.location.pathname)
  }
  window.__WUJIE_UNMOUNT = () => {
    instance?.unmount()
  }
}


